package stark.app.frame.recyleradapter;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.chad.library.adapter.base.listener.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import stark.app.R;
import stark.app.databinding.ActivityAdapterDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;

public class AdapterDemoActivity extends BaseNoModelActivity<ActivityAdapterDemoBinding> {

    DemoAdapter mAdapter;
    int start;

    @Override
    protected int onCreate() {
        return R.layout.activity_adapter_demo;
    }

    @Override
    protected void initView() {
        mAdapter = new DemoAdapter();
        mDataBinding.rv.setLayoutManager(new LinearLayoutManager(mContext));
        mDataBinding.rv.setAdapter(mAdapter);
        mAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.getLoadMoreModule().setEnableLoadMore(true);
                        List<AdapterDemoBean> beans = new ArrayList<>();
                        int num = start + 10;
                        for (; ; ) {
                            start++;
                            beans.add(new AdapterDemoBean(("标题" + start)));
                            if (start == num) break;
                        }
                        mAdapter.addData(beans);
                        mAdapter.getLoadMoreModule().loadMoreComplete();//加载状态有多种，详情请看LoadMoreModule
                    }
                }, 2000);


            }
        });
    }

    @Override
    protected void initData() {
        mDataBinding.setLifecycleOwner(this);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                List<AdapterDemoBean> beans = new ArrayList<>();
                int num = start + 10;
                for (; ; ) {
                    start++;
                    beans.add(new AdapterDemoBean(("标题" + start)));
                    if (start == num) break;
                }
                mAdapter.setNewInstance(beans);
                mDataBinding.swipeRefreshLayout.setRefreshing(false);
            }
        },2000);

        mDataBinding.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                start=0;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        List<AdapterDemoBean> beans = new ArrayList<>();
                        int num = start + 10;
                        for (; ; ) {
                            start++;
                            beans.add(new AdapterDemoBean(("标题" + start)));
                            if (start == num) break;
                        }
                        mAdapter.setNewInstance(beans);
                        mDataBinding.swipeRefreshLayout.setRefreshing(false);
                    }
                },2000);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        mDataBinding.swipeRefreshLayout.setRefreshing(true);

    }
}